package com.hzit.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.hzit.pojo.Classes;
import com.hzit.pojo.PageResult;
import com.hzit.pojo.Student;
import com.hzit.pojo.StudentVo;
import com.hzit.service.ClassesService;
import com.hzit.service.StudentService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@WebServlet("/student")
public class StudentServlet extends HttpServlet {
	private StudentService studentService;
	private ClassesService classesService;
	@Override
	public void init() throws ServletException {
		WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
		studentService = wac.getBean(StudentService.class);
		classesService = wac.getBean(ClassesService.class);
	}

	//1. 根据请求参数跳转到不同页面
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1.1 处理一下post请求的中文乱码
		req.setCharacterEncoding("UTF-8");
		//1.2 得到请求参数
		String cmd = req.getParameter("cmd");
		//1.3 根据参数调用不同的方法
		if(StrUtil.isNotBlank(cmd)){
			if ("list".equals(cmd)) {
				list(req,resp);
			}
			if("findByPage".equals(cmd)){
				findByPage(req,resp);
			}
			if("search".equals(cmd)){
				search(req,resp);
			}
			if("add".equals(cmd)){
				add(req,resp);
			}
			if("update".equals(cmd)){
				update(req,resp);
			}
			if("delete".equals(cmd)){
				delete(req,resp);
			}
		}
	}

	//2. 学生列表
	private void list(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException {
		//2.1 查询所有学生
		List<Student> studentList  = studentService.findAll();
		resp.setContentType("text/html;charset=utf8");
		resp.getWriter().println(JSON.toJSONString(studentList));
		resp.getWriter().flush();
		//2.2 放到请求作用域中
		// req.setAttribute("students",studentList);
		//2.3 进行页面的跳转
		// req.getRequestDispatcher("/index.jsp").forward(req,resp);
	}
	//3. 分页查询学生
	private void findByPage(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException {
		//3.1 得到分页参数
		int page = 1;
		int pageSize = 3;
		String pageStr = req.getParameter("page");
		String pageSizeStr = req.getParameter("pageSize");
		if(StrUtil.isNotBlank(pageStr)){
			page = Integer.parseInt(pageStr);
		}
		if(StrUtil.isNotBlank(pageSizeStr)){
			pageSize = Integer.parseInt(pageSizeStr);
		}
		//3.2 开始分页
		PageResult<Student> pr = studentService.findByPage(page,pageSize);
		//3.3 将其放到请求作用域中
		req.setAttribute("pr",pr);
		//3.4 跳转到列表页
		req.getRequestDispatcher("/index.jsp").forward(req,resp);
	}

	//4. 分页带条件查询学生
	private void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		getStudentInfo(req);
		//4.5 进行页面跳转
		req.getRequestDispatcher("/index.jsp").forward(req,resp);

	}

	private void getStudentInfo(HttpServletRequest req) {
		//4.1 得到分页参数
		int page = 1;
		int pageSize = 3;
		String pageStr = req.getParameter("page");
		String pageSizeStr = req.getParameter("pageSize");
		if(StrUtil.isNotBlank(pageStr)){
			page = Integer.parseInt(pageStr);
		}
		if(StrUtil.isNotBlank(pageSizeStr)){
			pageSize = Integer.parseInt(pageSizeStr);
		}
		//4.2 得到查询条件参数
		String sname = req.getParameter("sname");
		String addr = req.getParameter("addr");
		String cidStr = req.getParameter("cid");
		Integer cid = 0;
		if(StrUtil.isNotBlank(cidStr)){
			cid = new Integer(cidStr);
		}
		StudentVo vo = new StudentVo(sname,addr,cid);
		//4.3 进行条件带分页查询
		PageResult<Student> pr = studentService.search(page,pageSize,vo);
		req.setAttribute("pr",pr);
		//4.4 查询班级列表
		List<Classes> classes = classesService.findAll();
		req.setAttribute("classes",classes);
	}

	//5. 添加学生
	private void add(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException {
		//5.1 得到要添加的学生信息
		String sname = req.getParameter("sname");
		String sex = req.getParameter("sex");
		String addr = req.getParameter("addr");
		String age = req.getParameter("age");
		String cid = req.getParameter("cid");
		//5.2 将上面的提交过来的参数构造成学生对象
		Student student = new Student(sname,sex,new Integer(age),addr,new Integer(cid));
		//5.3 添加到数据库中
		studentService.add(student);
		//5.4 跳转到列表页面
		resp.sendRedirect("/student?cmd=search");

	}
	//6. 修改学生
	private void update(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException {
		//6.1 得到要添加的学生信息
		String sname = req.getParameter("sname");
		String sex = req.getParameter("sex");
		String addr = req.getParameter("addr");
		String age = req.getParameter("age");
		String cid = req.getParameter("cid");
		String sid = req.getParameter("sid");
		//6.2 构造要修改的学生对象
		Student student = new Student(new Integer(sid),sname,sex,new Integer(age),addr,new Integer(cid));
		//6.3 完成学生的修改
		studentService.update(student);
		//6.4 跳转到列表页面
		resp.sendRedirect("/student?cmd=search");
	}
	//6. 删除学生
	private void delete(HttpServletRequest req, HttpServletResponse resp)   throws ServletException, IOException {
		//6.1 得到要删除的学生编号
		String sid = req.getParameter("sid");
		//6.2 执行删除
		studentService.delete(sid);
		//6.3 跳转到列表页面
		resp.sendRedirect("/student?cmd=search");
	}
}
